package com.lzy;

import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * @author lzy by 2019/07/10
 */
@Data
@EqualsAndHashCode
public class RestResponseResult<T> {
    private static final int SUCCESS = 200;

    private static final int FAIL = 500;
    /**
     * 状态码
     */
    private Integer code;
    /**
     * 消息
     */
    private String message;
    /**
     * 调用时间
     */
    private Long timestamp = System.currentTimeMillis();
    /**
     * 数据
     */
    private T data;
    /**
     * 请求id
     */
    private String requestId;

    /*public RestResponseResult(Integer code, String message, Object o) {
        this.code = code;
        this.message = message;
        this.data = (T)o;
    }*/


    public static <T> RestResponseResult<T> failResponse(Integer code, String message) {
        RestResponseResult<T> response = new RestResponseResult<>();
        response.setCode(code);
        response.setMessage(message);
        return response;
    }

    public static <T> RestResponseResult<T> failResponse(String message) {
        RestResponseResult<T> response = new RestResponseResult<>();
        response.setCode(FAIL);
        response.setMessage(message);
        return response;
    }

    public static <T> RestResponseResult<T> failResponse(Integer code, String message, T t) {
        RestResponseResult<T> response = new RestResponseResult<>();
        response.setCode(code);
        response.setMessage(message);
        response.setData(t);
        return response;
    }

    /*public static <T> RestResponseResult<T> failResponse(ResultCodeEnum codeEnum) {
        RestResponseResult<T> response = new RestResponseResult<>();
        response.setCode(codeEnum.getCode());
        response.setMessage(codeEnum.getText());
        return response;
    }

    public static <T> RestResponseResult<T> failResponse(ResultCodeEnum codeEnum, T t) {
        RestResponseResult<T> response = new RestResponseResult<>();
        response.setCode(codeEnum.getCode());
        response.setMessage(codeEnum.getText());
        response.setData(t);
        return response;
    }*/

    public static <T> RestResponseResult<T> success() {
        RestResponseResult<T> response = new RestResponseResult<>();
        response.setCode(SUCCESS);
        return response;
    }

    public static <T> RestResponseResult<T> success(T t) {
        RestResponseResult<T> response = new RestResponseResult<>();
        response.setCode(SUCCESS);
        response.setData(t);
        return response;
    }


    public boolean isSuccess() {
        return this.code.compareTo(SUCCESS) == 0;
    }

    public boolean isFail() {
        return !isSuccess();
    }
    /**
     * 参数验证失败返回结果
     *//*
    public static <T> RestResponseResult<T> validateFailed() {
        return failResponse(ResultCode.VALIDATE_FAILED);
    }*/
/*
    *//**
     * 参数验证失败返回结果
     * @param message 提示信息
     *//*
    public static <T> RestResponseResult<T> validateFailed(String message) {
        return new RestResponseResult<T>(ResultCode.VALIDATE_FAILED.getCode(), message, null);
    }

    *//**
     * 未登录返回结果
     *//*
    public static <T> RestResponseResult<T> unauthorized(T data) {
        return new RestResponseResult<T>(ResultCode.UNAUTHORIZED.getCode(), ResultCode.UNAUTHORIZED.getMessage(), data);
    }

    *//**
     * 未授权返回结果
     *//*
    public static <T> RestResponseResult<T> forbidden(T data) {
        return RestResponseResult..code(ResultCode.FORBIDDEN.getCode())ResultCode.FORBIDDEN.getMessage(), data);
    }*/

    
}
